from django.shortcuts import render, redirect,HttpResponse,HttpResponseRedirect
from django.urls import reverse

from app01.forms import DeviceTypeForm,SubProcessForm
from app01.models import DeviceType,Device,Employees,Process,EmployeeHours,MachineHours,SubProcess

# Create your views here.


# 首页


def home_page(request):
    context = {

    }
    return render(request, 'app01/homepage.html', context)


def add_devicetype(request):
    dt_form = DeviceTypeForm()

    if request.method == 'POST':
        dt_form = DeviceTypeForm(request.POST)
        if dt_form.is_valid():
            name_get = dt_form.cleaned_data['typename']

            devicetype = DeviceType.objects.create(typename=name_get)
            devicetype.save()

            return redirect('index')


    context = {
        'dt_form':dt_form,

    }
    return render(request, 'app01/add_devicetype.html', context)


def devicetype_list(request):
    devicetype = DeviceType.objects.all()

    context = {
        "devicetype":devicetype,

    }
    return render(request,'app01/devicetype_list.html',context)


def device_list(request):
    device = Device.objects.all()

    context = {
        "devices_list":device
    }
    return render(request,'app01/device_list.html',context)


def process_list(request):
    processes = Process.objects.all()

    context = {
        "processes":processes,
    }
    return render(request, 'app01/process_list.html', context)


def employee_list(request):
    employees = Employees.objects.all()

    context = {
        "employees":employees,
    }
    return render(request,'app01/employee_list.html',context)


def employee_hours(request):
    employee_hours_list = EmployeeHours.objects.all()

    context = {
        "employee_hours_list":employee_hours_list,
    }
    return render(request,'app01/employee_hours.html',context)


def machine_hours(request):
    machine_hours_list = MachineHours.objects.all()

    context = {
        "machine_hours_list":machine_hours_list,
    }
    return render(request,'app01/machine_hours.html',context)


def view_subprocess(request,process_id):
    s_form = SubProcessForm()
    error_value =1   # 默认状态值为1
    subprocess_list = SubProcess.objects.filter(process_id=process_id)


    if subprocess_list.count() == 0:    # 如果查询结果为空，则把错误状态值以为0
        error_value = 0
        # print(f"数据库中无记录:{error_value}")

    if request.method == 'POST':
        if "update-btn" in request.POST:
            print(request.POST)
            return HttpResponse('ok')

        s_name_get = request.POST.get("subprocessName")
        print(s_name_get,process_id)
        subprocess = SubProcess.objects.create(name=s_name_get,process_id=process_id)
        subprocess.save()
        return HttpResponse('ok')

    context = {
        "subprocess_list":subprocess_list,
        "error_value":error_value,
        "process_id":process_id,
        "s_form":s_form,
    }
    return render(request,'app01/view_subprocess.html',context)


def update_subprocess(request,subprocess_id):
    s_obj = SubProcess.objects.get(id=subprocess_id)
    # s_form = SubProcessForm(instance=s_obj)


    if request.method =="POST":
        print(request.POST)
        s_form = SubProcessForm(request.POST)
        if s_form.is_valid():
            name_get = s_form.cleaned_data['name']
            process_get = s_form.cleaned_data['process']
            subprocess = SubProcess.objects.filter(id=s_obj.id)
            subprocess.update(id=subprocess_id,name=name_get,process=process_get)

            # 使用HttpResponseRedirect返回经过reverse反向解析URL并添加了参数值的页面，使之能够准确返回对应的父页面
            return HttpResponseRedirect(reverse('view_subprocess',args=[str(process_get.id)]))
    else:
        s_form = SubProcessForm(instance=s_obj)


    context = {
        "s_form":s_form,
        "s_obj":s_obj,
    }
    return render(request,'app01/update_subprocess.html',context)


def del_subprocess(request):
    del_id = request.POST.get("id")  # 获取请求中的id值
    SubProcess.objects.filter(id=del_id).delete()   # 根据id值删除数据库表中对应记录

    return HttpResponse("删除成功")



def add_supprocess(request,process_id):
    s_form = SubProcessForm()

    if request.method == 'POST':
        s_form = SubProcessForm(request.POST)
        if s_form.is_valid():
            name_get = s_form.cleaned_data['name']
            process_get = s_form.cleaned_data['process']

            subprocess = SubProcess.objects.create(name=name_get,process=process_get)
            subprocess.save()

            return redirect('view_subprocess')


    context = {
        's_form':s_form,
        'process_id':process_id,
    }
    return render(request,'app01/add_supprocess.html',context)

